//
//  LoginViewController.m
//  UI 作业 - 豆瓣电影
//
//  Created by hesiyao on 15/12/23.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "registerViewController.h"
#import "DataBaseManager.h"
#import "ContactModel.h"
@interface LoginViewController () <pushVC,backDelegate>
@property (nonatomic, strong) LoginView *backView;
@property (nonatomic, strong) NSMutableArray *nameArr;
@property (nonatomic, strong) NSMutableArray *passwordArr;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.backView = [[[NSBundle mainBundle] loadNibNamed:@"loginView" owner:self options:nil] lastObject];
    [self.view addSubview:_backView];
    
    // 设置代理
    _backView.delegate = self;
    
    //navigationBar 设置
    self.navigationItem.title = @"用户登陆";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];

    
}

/// 实现登陆按钮的跳转
- (void)login:(NSString *)name pass:(NSString *)password
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *userName = [user objectForKey:@"userName"];
//    NSString *passWord = [user objectForKey:@"passWord"];
    // 查询所有的用户
    [[DataBaseManager shareInstance] openDB];
    NSMutableArray *arr = [[DataBaseManager shareInstance] select];
    
    for (ContactModel *m in arr) {
        NSLog(@"%@, %@", m.contactName, m.password);
        // 如果用户名和密码都正确就登陆成功并改变状态为登陆 将
        if ([m.contactName isEqualToString:name] && [m.password isEqualToString:password]) {
            [user setBool:NO forKey:@"button"];
//            BOOL b = [[NSUserDefaults standardUserDefaults] boolForKey:@"button"];
            
            [user setBool:YES forKey:@"didLogin"];
            // 将已经登陆成功的用户名存在单例中
            [user setObject:userName forKey:@"userName"];
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
            // 结束循环遍历
            return;
            //如果用户名存在但是密码不正确就提示密码错误
        }if ([m.contactName isEqualToString:name] && ![m.password isEqualToString:password]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码错误!" preferredStyle:UIAlertControllerStyleAlert];
            __weak UIAlertController *art = alert;
            [self presentViewController:alert animated:YES completion:^{
                [art dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }];
        }
        
        //将所有的名字都放入一个数组保存,将name和这里的用户名进行逐一比较 如果都不相同就显示此用户名不存在
        self.nameArr = [NSMutableArray array];
        [_nameArr addObject:m.contactName];
        
        
    }
    //将所有的名字都放入一个数组保存,将name和这里的用户名进行逐一比较 如果都不相同就显示此用户名不存在
    for (NSString *temp in _nameArr) {
        if (![name isEqualToString:temp]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"此用户名不存在!" preferredStyle:UIAlertControllerStyleAlert];
            __weak UIAlertController *art = alert;
            [self presentViewController:alert animated:YES completion:^{
                [art dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }];
        }
    }
    
    
}


/// 实现注册页面的跳转

-(void)push
{
    registerViewController *registerVC = [[registerViewController alloc] init];
    // 返回上一页的代理
    registerVC.backDelegate = self;
    
    [self.navigationController pushViewController:registerVC animated:YES];
    NSLog(@"1");
}

/// 返回上一个界面的代理方法
-(void)backPage
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];

}

/// 返回上一页实现
-(void)back
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
